package com.xinqing.test.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Author:CHQ
 * @Date:2020/8/28 15:43
 * @Description
 */
public class Tools {
    public static String MD5(String input) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 加密失败");
        }
        byte[] bytes = input.getBytes();
        byte[] md5Bytes = md5.digest(bytes);
        StringBuilder hexValue = new StringBuilder();
        for (int i = 0; i < md5Bytes.length; i++) {
            int str = md5Bytes[i] & 0xff;
            if (str < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(str));
        }
        return hexValue.toString();
    }
}
